// Modules to control application life and create native browser window
const { app, BrowserWindow ,dialog} = require('electron')
const path = require('node:path')
 
  
function createWindow () {
  var mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    show:false,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      sandbox:false,
      contextIsolation:true,
    }
  })
  mainWindow.webContents.setWindowOpenHandler( 传入数据 =>{ } )
  mainWindow.loadFile(path.join(__dirname, 'index.html'))
   
  mainWindow.on("ready-to-show",()=>{ mainWindow.show() })
  // mainWindow.webContents.openDevTools()
}






//https://www.electronjs.org/zh/docs/latest/tutorial/launch-app-from-url-in-another-app
//🍍🍍🍍 深度链接(Deep Links)(打包中需要设置 protocols)   捕获 系统级别 的链接
function 注册深度链接(deep_link_name){
  console.log("🍍🍍🍍注册深度链接", process.defaultApp,  process.argv)
  if (process.defaultApp) {  
      if (process.argv.length >= 2) {
        app.setAsDefaultProtocolClient(deep_link_name, process.execPath, [path.resolve(process.argv[1])])
      }
  } else {
      app.setAsDefaultProtocolClient(deep_link_name)
  }
  app.on('open-url', (event, url) => {
    console.log('欢迎回来', `导向自: ${url}`)
  })
}
 
 






(async ()=>{
  const 唯一应用锁 = app.requestSingleInstanceLock()
  if (!唯一应用锁) {  app.quit()  }  
  else{

  }
  // 创建主窗口，加载应用程序的其他部分，等等...
  app.whenReady().then(() => {  console.log("whenReady", 唯一应用锁) ; if(唯一应用锁) createWindow()   })

  let num = 0
  app.on('second-instance', (event, commandLine, workingDirectory) => {
    // 用户正在尝试运行第二个实例，我们需要让焦点指向我们的窗口
    createWindow()
    console.log(`深度链接 ${num++}`, commandLine)
  })

  app.on('window-all-closed', function () {  if (process.platform !== 'darwin') app.quit()   })
  注册深度链接("lwleentest")
})()

 